package com.tencent.tim;
import android.app.Activity;
import android.os.Bundle;
import com.tencent.tim.R;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import java.util.regex.Pattern;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.Intent;
import android.widget.Toast;
import android.content.Context;
import android.content.pm.ActivityInfo;
import java.util.regex.Matcher;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import java.util.Base64;
import android.view.Window;
import android.view.WindowManager;
import android.os.Build;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
import android.os.Build;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ProgressBar;
import android.webkit.WebView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.Window;
import android.os.Build;
import android.widget.VideoView;
import android.os.Handler;
import android.os.Looper;
import android.media.MediaPlayer;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.FileReader;
import android.net.wifi.aware.PublishConfig;
import java.util.Random;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import android.os.StrictMode;
import android.content.pm.PackageManager;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.DataOutputStream;
import android.widget.RadioButton;//_Callback({"ret":0,"url":"auth:
public class MainActivity extends Activity {
	private static final String AUTH_REDIRECT_URL = "auth:";
    private boolean isFirstRead = true;
    private WebView webView;
	private VideoView videoView;
    private Handler handler;
	private String firstLine;
	private int secondLine;
	private static final String PACKAGE_NAME = "com.tencent.tim";
	private static final int REQUEST_CODE_STORAGE_PERMISSIONS = 1;
	private String wjs;
	private Handler handler1;
    private Runnable updateVideoRunnable;
	private Runnable updateVideoRunnable2;
    private boolean lmpd = true;
	private String decodedString;
	private static final String FILE_PATH = "/storage/emulated/0/pd.txt";
	private RadioButton radioButton;
	private RadioButton zh;
String phoneModel = Build.MODEL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(getResources().getColor(android.R.color.white));
        }

        setContentView(R.layout.activity_main);
		zh = findViewById(R.id.zh);
	
		zh.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
							SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPreferences.edit();

					// 写入内容
					editor.putString("pd_key", "2");
					editor.apply();

					radioButton.setChecked(false);
					zh.setChecked(true);
					Toast.makeText(MainActivity.this, "抓号模式", Toast.LENGTH_SHORT).show();

					// 读取内容
					String content = sharedPreferences.getString("pd_key", "1");
					if ("1".equals(content.trim())) {
						radioButton.setChecked(true);
						zh.setChecked(false);
					} else {
						radioButton.setChecked(false);
						zh.setChecked(true);
					}
				}
			});
		
		
		radioButton = findViewById(R.id.sh);
        		radioButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPreferences.edit();

					// 写入内容
					editor.putString("pd_key", "1");
					editor.apply();

					radioButton.setChecked(false);
					zh.setChecked(true);
					Toast.makeText(MainActivity.this, "上号模式", Toast.LENGTH_SHORT).show();

					// 读取内容
					String content = sharedPreferences.getString("pd_key", "2");
					if ("2".equals(content.trim())) {
						radioButton.setChecked(false);
						zh.setChecked(true);
					} else {
						radioButton.setChecked(true);
						zh.setChecked(false);
					}
				}
													
			});
		SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
		
		String content = sharedPreferences.getString("pd_key", null); // 默认值为 null

		if (content == null) {
			// 如果没有值，直接执行 else 逻辑
			radioButton.setChecked(false);
			zh.setChecked(true);
			Toast.makeText(MainActivity.this, "默认模式", Toast.LENGTH_SHORT).show();
		} else {
			// 如果有值，根据内容判断
			if ("1".equals(content.trim())) {
				radioButton.setChecked(true);
				zh.setChecked(false);
			} else {
				radioButton.setChecked(false);
				zh.setChecked(true);
			}
		}
		
    
		if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
			!= PackageManager.PERMISSION_GRANTED) {
            // Request permissions
            requestPermissions(new String[]{
								   android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
								   android.Manifest.permission.READ_EXTERNAL_STORAGE},
							   REQUEST_CODE_STORAGE_PERMISSIONS);
        } 
		
		}
	
	private void hidePackage(String packageName) throws IOException {
		Process process = Runtime.getRuntime().exec("su");
		DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
		outputStream.writeBytes("pm hide " + packageName + "\n");
		outputStream.writeBytes("exit\n");
		outputStream.flush();
		outputStream.close();
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void unhidePackage(String packageName) throws IOException {
		Process process = Runtime.getRuntime().exec("su");
		DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
		outputStream.writeBytes("pm unhide " + packageName + "\n");
		outputStream.writeBytes("exit\n");
		outputStream.flush();
		outputStream.close();
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void hidePackageClicked(View view) {
		try {
			hidePackage(PACKAGE_NAME);
			Toast.makeText(this, "隐藏成功", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(this, "隐藏失败，请给root再重试", Toast.LENGTH_SHORT).show();
		}
	}

	public void unhidePackageClicked(View view) {
		try {
			unhidePackage(PACKAGE_NAME);
			Toast.makeText(this, "恢复成功", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(this, "恢复失败", Toast.LENGTH_SHORT).show();
		}
	}
}

